package com.googlecode.animator.data.objects3d
{
	import com.googlecode.animator.data.IValueObject;
	import com.googlecode.animator.data.materials.BitmapFileMaterialVO;
	import com.googlecode.animator.data.materials.ColorMaterialVO;
	import com.googlecode.animator.data.materials.MaterialVO;
	import com.googlecode.animator.data.materials.MovieMaterialVO;
	import com.googlecode.animator.data.materials.VideoStreamMaterialVO;
	
	public class SphereVO extends Object3DVO
	{
		
		public static const TYPE:String = "sphere";
		
		public static const FRONT_FACE:String = "front";
		
		public var radius:Number = 250;
		
		public var segmentsW:uint = 4;
		
		public var segmentsH:uint = 4;
		
		public var material:MaterialVO;
		
		public function SphereVO()
		{
			label = "Default Sphere";
		}
		
		override public function clone(src:* = null):IValueObject
		{
			var clone:IValueObject = super.clone(new SphereVO());
			
			(clone as SphereVO).radius = this.radius;
			(clone as SphereVO).segmentsH = this.segmentsH;
			(clone as SphereVO).segmentsW = this.segmentsW;
			(clone as SphereVO).material = this.material;
			
			return (clone as SphereVO);
		}
		
		override public function toXML(src:XML = null):XML
		{
			var xml:XML = super.toXML(new XML(<{TYPE} name={this.name} label={this.label}></{TYPE}>));
			
			xml.appendChild(<radius>{this.radius}</radius>);
			xml.appendChild(<material>{this.material.toXML()}</material>);

			return xml;
		}
		
		override public function fromXML(src:XML):void
		{
			super.fromXML(src);
			
			this.radius = src.radius;
			
			var newMat:MaterialVO;
			
			if(src.material.name() == ColorMaterialVO.TYPE)
			{
				newMat = new ColorMaterialVO();
				(newMat as ColorMaterialVO).fillAlpha = src.material.fillAlpha;
				(newMat as ColorMaterialVO).fillColor = src.material.fillColor;
				 
			}
			
			if(src.material.name() == MovieMaterialVO.TYPE)
			{
				newMat = new MovieMaterialVO();
				(newMat as MovieMaterialVO).filePath = src.material.filePath;
 
			}
			
			if(src.material.name() == BitmapFileMaterialVO.TYPE)
			{
				newMat = new BitmapFileMaterialVO();
				(newMat as BitmapFileMaterialVO).filePath = src.material.filePath;
 
			}
			
			if(src.material.name() == VideoStreamMaterialVO.TYPE)
			{
				newMat = new VideoStreamMaterialVO();
				(newMat as VideoStreamMaterialVO).filePath = src.material.filePath;
				(newMat as VideoStreamMaterialVO).width = src.material.width;
				(newMat as VideoStreamMaterialVO).height = src.material.height;

			}
			
			this.material = material;
		}
	}
}